package tech.daoxuan.web.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import tech.daoxuan.web.filter.TimeFilter;
import tech.daoxuan.web.interceptor.TimeInterceptor;

import java.util.ArrayList;
import java.util.List;

/**
 * 把timeFilter作为第三方 filter 加进去，用这种方式，注册到IOC容器中。
 *
 * 可以指定 过滤路径
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private TimeInterceptor timeInterceptor;
    /**
     * {@inheritDoc}
     * <p>This implementation is empty.
     *  拦截器的注册器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        super.addInterceptors(registry);

        registry.addInterceptor(timeInterceptor);
    }

    @Bean
    public FilterRegistrationBean timeFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

        TimeFilter timeFilter = new TimeFilter();

        filterRegistrationBean.setFilter(timeFilter);

        List urls = new ArrayList();

        urls.add("/*");
        filterRegistrationBean.setUrlPatterns(urls);

        return filterRegistrationBean;
    }

    /**
     * {@inheritDoc}
     * <p>This implementation is empty.
     *  异步时，加载 拦截器
     * @param configurer
     */
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
//        /configurer.registerDeferredResultInterceptors()
        super.configureAsyncSupport(configurer);
    }
}
